package com.example.shardingspheredemo.model.t.controller;

import com.example.shardingspheredemo.model.t.entity.BookEntity;
import com.example.shardingspheredemo.model.t.entity.UserEntity;
import com.example.shardingspheredemo.model.t.service.BookService;
import com.example.shardingspheredemo.model.t.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Andrew Yang
 * @className UserController
 * @description
 * @package com.example.shardingspheredemo.model.t.controller
 * @create 2020-09-04 15:18
 */
@RestController
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    /**
     * 模拟插入数据
     */
    List<UserEntity> userList = new ArrayList<>();

    /**
     * 初始化插入数据
     */
    @PostConstruct
    private void getData() {
        userList.add(new UserEntity("大大", 4, 8));
        userList.add(new UserEntity("小小", 3, 3));
        userList.add(new UserEntity("爸爸", 31, 1));
        userList.add(new UserEntity("妈妈", 28, 0));
        userList.add(new UserEntity("爷爷", 63, 2));
        userList.add(new UserEntity("奶奶", 62, 7));
    }

    /**
     * @Description: 批量保存用户
     */
    @PostMapping("save-user")
    public Object saveUser() {
        return userService.insertForeach(userList);
    }

    /**
     * @Description: 获取用户列表
     */
    @GetMapping("list-user")
    public Object listUser() {
        return userService.getList();
    }

    @GetMapping("save-book")
    public Object saveBook() {
        return bookService.insertForEach(Arrays.asList(
                new BookEntity("MYSQL从入门到入土",56),
                new BookEntity("JAVA Head First",99)
                ));
    }
}
